package thirt.view.load
{
  import flash.display.Bitmap;
  import flash.display.Shape;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.ProgressEvent;
  import flash.text.TextField;
  
  import mx.events.FlexEvent;
  import mx.preloaders.IPreloaderDisplay;
  import mx.preloaders.Preloader;
  public class Preload extends Sprite implements IPreloaderDisplay
  {
  	[Embed(source="style/loading/LoadingBg.png")]
 	private var LoaderMC:Class;
 	private var _loader_mc:Bitmap;
 	
 	[Embed(source="style/loading/logoBlue.png")]
 	private var bluePre:Class;
 	private var _loader_Bule:Bitmap;
 	
 	[Embed(source="style/loading/logoCCCCCC.png")]
 	private var defaultPre:Class;
 	private var _loader_Default:Bitmap;
 
 	private var _preloader:Preloader;
 	
 	private var shape:Shape;
 
	private var sp:Sprite; 
	
	private var txt:TextField;
    public function Preload()
    {
      super();
      sp = new Sprite;
      this.addChild(sp);
      
      this._loader_mc = new LoaderMC()
      sp.addChild(this._loader_mc);
      
      _loader_Default = new defaultPre();
      _loader_Default.x = 201;
      _loader_Default.y = 249;
      sp.addChild(_loader_Default);
      
      _loader_Bule = new bluePre();
      _loader_Bule.x = 201;
      _loader_Bule.y = 249;
      sp.addChild(_loader_Bule);
      
      shape = new Shape;
      shape.x = 201;
      shape.y = 249;
      sp.addChild(shape);
      _loader_Bule.mask = shape;
      
      txt = new TextField;
      txt.width = 270;
      txt.height = 20;
      txt.x = 200;
      txt.y = 360;
      sp.addChild(txt);
    }
    
    public function get backgroundAlpha():Number
    {
      return 0;
    }
    
    public function set backgroundAlpha(value:Number):void
    {
    }
    
    public function get backgroundColor():uint
    {
      return 0;
    }
    
    public function set backgroundColor(value:uint):void
    {
    }
    
    public function get backgroundImage():Object
    {
      return null;
    }
    
    public function set backgroundImage(value:Object):void
    {
    }
    
    public function get backgroundSize():String
    {
      return null;
    }
    
    public function set backgroundSize(value:String):void
    {
    }
    
    public function set preloader(obj:Sprite):void
    {
      _preloader = obj as Preloader;
	  _preloader.addEventListener(ProgressEvent.PROGRESS, progressEventHandler);
	  _preloader.addEventListener(FlexEvent.INIT_COMPLETE,initCompleteEventHandler);
    }
    
    public function get stageHeight():Number
    {
      return 0;
    }
    
    public function set stageHeight(value:Number):void
    {
    }
    
    public function get stageWidth():Number
    {
      return 0;
    }
    
    public function set stageWidth(value:Number):void
    {
    }
    
    public function initialize():void
    {
      sp.x = stage.stageWidth / 2 - _loader_mc.width/2;
      sp.y = stage.stageHeight / 2 - _loader_mc.height/2;
    }
    
    private function progressEventHandler(eo:ProgressEvent):void
    {
      var pre:Number = eo.bytesLoaded / eo.bytesTotal;
      shape.graphics.clear();
      shape.graphics.beginFill(0xff0000);
      shape.graphics.drawRect(0,0,pre*279,78);
      shape.graphics.endFill();
     // _loader_mc.gotoAndStop(Math.round((eo.bytesLoaded / eo.bytesTotal )*100))
     // _loader_mc.show_txt.text =Math.round((eo.bytesLoaded / eo.bytesTotal )*100)+" %"
     txt.htmlText = "<p align='center'><font color='#006699' face='微软雅黑'>Loading " + Math.round((eo.bytesLoaded / eo.bytesTotal )*100) +  "%</font></p>"
    }
    
    private function initCompleteEventHandler(eo:FlexEvent):void{
      dispatchEvent(new Event(Event.COMPLETE));
    }
    
    
  }
}